package ro.gateway.aida.servlet;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import ro.gateway.aida.obj.Activity;
import ro.gateway.aida.obj.AidaResource;
import ro.gateway.aida.utils.HttpUtils;
import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.TranslatorUtils;



/**
 * Created by IntelliJ IDEA.
 * User: xblue
 * Date: Mar 2, 2004
 * Time: 2:41:04 PM
 * To change this template use Options | File Templates.
 */
public class EditWebResourcesAction extends Action
{
      public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest request,
            HttpServletResponse response )
            throws Exception
      {
            HttpSession session = request.getSession();
            String action = HttpUtils.getValidTrimedString( request, "action", null );
            String key = HttpUtils.getValidTrimedString( request, "key", null );
            Hashtable ed_ctxs = ( Hashtable ) session.getAttribute( EditActivityServlet.ED_CTXS );
            Hashtable ctx = ( key != null && ed_ctxs != null ) ? ( Hashtable ) ed_ctxs.get( key ) : null;
            Activity activity = ( ctx != null ) ? ( Activity ) ctx.get( EditActivityServlet.PNAME_BEAN ) : null;
            LanguageBean lang = TranslatorUtils
                  .getCurrentLanguage( request, this.getServlet().getServletContext() );

            if ( null == action || null == key || activity == null )
            {
                  return mapping.findForward( "closeme" );
            }


            String s_idxs = HttpUtils.getValidTrimedString( request, "idxs", null );
            if ( s_idxs == null ) s_idxs = "";
            request.setAttribute( "idxs", s_idxs );
            int idxs[] = parse_idxs( s_idxs );
            Arrays.sort( idxs );
            request.setAttribute( "wr_idxs", idxs );
            ArrayList items = activity.getResourcesList();
            request.setAttribute( "wr_items", items );


            if ( "edit".equals( action ) )
            {

            }
            else if ( "save".equals( action ) )
            {
                  for ( int i = 0; i < idxs.length; i++ )
                  {
                        AidaResource resource = ( AidaResource ) items.get( idxs[ i ] );
                        String url = HttpUtils.getValidTrimedString( request, "url" + idxs[ i ], null );
                        String title = HttpUtils.getValidTrimedString( request, "title" + idxs[ i ], null );
                        String description = HttpUtils.getValidTrimedString( request, "description" + idxs[ i ], null );
                        if ( !url.startsWith( "http://" ) && !url.startsWith( "ftp://" ) )
                        {
                              url = "http://" + url;
                        }
                        resource.setLink( url );
                        resource.setDesc( lang.language, description );
                        resource.setTitle( lang.language, title );
//                        System.out.println( "set desc:" + description
//                              + "(" + lang.language + "):for:" + url );
                  }
                  return mapping.findForward( "closeme" );
            }
            else if ( "delete".equals( action ) )
            {
                  for ( int i = idxs.length; i >= 0; i-- )
                  {
                        items.remove( idxs[ i ] );
                  }
            }
            else if ( "create".equals( action ) )
            {
                  AidaResource item = new AidaResource();
                  items.add( item );
                  idxs = new int[]{items.size() - 1};
                  request.setAttribute( "wr_idxs", idxs );
                  request.setAttribute( "idxs", String.valueOf( idxs[ 0 ] ) );
            }
            else if ( "cancel_create".equals( action ) )
            {
                  if ( items.size() > 0 )
                  {
                        items.remove( items.size() - 1 );
                  }
                  return mapping.findForward( "closeme" );
            }
            else
            {
                  return mapping.findForward( "closeme" );
            }


            return mapping.findForward( "jsp_page" );
      }

      private static int[] parse_idxs( String s_idxs )
      {
            int[] result = new int[ 0 ];
            if ( s_idxs == null ) return result;

            StringTokenizer st = new StringTokenizer( s_idxs, "," );
            while ( st.hasMoreTokens() )
            {
                  String idx = st.nextToken();
                  try
                  {
                        int idx_ = Integer.parseInt( idx );
                        int[] new_result = new int[ result.length + 1 ];
                        if ( result.length > 0 )
                              System.arraycopy( result, 0, new_result, 0, result.length );
                        new_result[ result.length ] = idx_;
                        result = new_result;
                  }
                  catch ( Throwable ex )
                  {

                  }
            }

            return result;
      }
}
